package corewarsxxi.logic;

import java.io.Serializable;

/**
 *
 * @author ei10139
 */
public class AMIndirect extends AddressingMode implements Serializable
{
    /** Simplefied Singleton that provides access to the public final static <code>AMImmediate</code> instance.*/
    public static final AMIndirect self = new AMIndirect();

    /**
     * Protected constructor of AMIndirect.
     */
    protected AMIndirect() {}

    /**
     * Calculates the value according to the current state of the memory array.
     * @param value     the value.
     * @param aField    look into either A-Field of B-Field.
     * @return          the value from Cores memory.
     */
    public int getValue(int value, boolean aField) {
        Core c = Core.getInstance();
        int offset = c.currentWarrior.processes.peek().offset;
        return (offset + value + (aField ?
                                    c.memArray.get(offset + value).aField.value :
                                    c.memArray.get(offset + value).bField.value) ) % c.getSize();
    }
    
    /**
     * Overrides the default Object.toString() function.
     * @return Always returns "<code>*@</code>".
     */
    @Override
    public String toString()
    {
        return "*@"; // A field has the simbol *, B field has @
    }

    /**
     * Makes use of the Singleton pattern and returns the public static object.
     * @return it<i>self</i>.
     */
    @Override
    public AddressingMode getSelf() {
        return this.self;
    }
}
